/**
 ********************************************************************
 * @author roocket
 * @file    main.c
 * @version V0.0.0
 * @date    2024/3/22
 * @brief   This file is to indicate main functions. 
 * @attention Code file rules:
 * rule: file encoding use UTF8;
 * rule: max line length 120 characters;
 * rule: line separator \r\n;
 * rule: use clion auto code format tool.
 */
#include "curl.h"
#include <pthread.h>

/* for users */
#define CURL_DEFAULT_BKP 0
#define CURL_NORMAL_FILE false
#define CURL_APPEND_FILE true

size_t test_get_progress_bar(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
{
    float percent;
    static float last_percent;

    percent = 100 * ((float)dlnow / (float)dltotal);
    if (dltotal > 0.0 && last_percent != percent) {
        printf("[pthread : %p] %.2f%%\n", clientp, percent);
        last_percent = percent;
    }
    return 0;
}

void test_get_spent_time(const char *pthreads, struct timeval *s, struct timeval *f)
{
    printf("The ptheads info is [%s], the delta of spent time %ld s, %d us\n", pthreads, f->tv_sec - s->tv_sec, abs(f->tv_usec - s->tv_usec));
    return;	
}

void *app_main(void *a)
{
    char url[150] = "http://219.135.177.242:30073/czi/mp10/position.yuv";
    int err = -1;

    struct curl_user_hdl curl = 
    {
        .curl_get_progress_bar = test_get_progress_bar,
        .curl_is_break_point = CURL_NORMAL_FILE,
        .curl_break_point = CURL_DEFAULT_BKP,
        .curl_file_location = "./ok1.zip",
        .curl_get_spent_time = test_get_spent_time,
        .curl_timeout = 60,
        .curl_limit_speed_kb = 100000,
        .curl_is_multiple = true
    };

    /* get the size of file from server */
    printf("the url file size %ld\n", curl_get_url_file_size(url));

    /* get the md5 of file */
    /* //@fixme
        curl_get_md5_of_file(url);
    */

    err = curl_download_file(url, &curl);
    if (-1 == err) {
        perror("curl download file error");
        return NULL;
    }

    printf("welcome to use libcurl by roocket\n");
    return NULL;
}

int main()
{
    pthread_t a;
    pthread_create(&a, NULL, app_main, NULL);
    while(1) {
        ;
    }
    return 0;
}